package Main;

import java.awt.Graphics;

import Other.Field;
import Robots.Robot1;
import Units.Unit;
import Units.UnitName;
import jgame.JGColor;
import jgame.JGImage;
import jgame.JGObject;
import jgame.JGPoint;
import jgame.platform.JGEngine;

public class Game extends JGEngine {
	public static Board board = null;
	public static Game game = null;
	
	public static void main(String[] args){
		game = new Game(new JGPoint(960, 640));
		game.setGameState("NewGame");
	}
	
	/** Application constructor. */
	public Game(JGPoint size) { initEngine(size.x,size.y); }

	/** Applet constructor. */
	public Game() { initEngineApplet(); }

	@Override
	public void initCanvas() {
		setCanvasSettings(80,80,12,8,JGColor.black,new JGColor(255,246,199),null);		
	}
	
	public int x = 0, y = 0;
	public boolean crtaj = true;
	public Robot1 robot1;

	@Override
	public void initGame() {
		removeObjects(null, 0);
		setFrameRate(35, 2);
		defineMedia("dvr.tbl");
		setBGImage("background");
		board = new Board();
		setGameState("InGame");
	}
	
	public void doFrameInGame() {
		if (board.isGameOver()){			
			setGameState("GameOver");
			return ;
		}
		
		moveObjects();
		killUnits();
		board.playSound();
		
		if (getMouseButton(1) && !board.getAnimation()){
			clearMouseButton(1);
			x = getMouseX();
			y = getMouseY();
			crtaj = true;

			if (board.getCurrentStep() == 0) checkAllowed(false);
			else if (board.getCurrentStep() == 1){
				Field f = board.getField(getMouseX(), getMouseY());
				int cid = board.getCID(f.i, f.j);
				
				if (cid == board.getSelectedUnitID()){
					board.changeTurn();
				}
				else if (board.isAllowed(f.i, f.j)){
					// if clicked on allowed field
					// move unit to that field
					Unit unit = board.getSelectedUnit();
					unit.goTo(f);
				}
			}
		}

		board.resetFieldsView();
	}
	
	public void paintFrameInGame(){
		
	}
	
	public void doFrameGameOver(){
		if (getKey('Y')){
			clearKey('Y');
			removeGameState("GameOver");
			setGameState("NewGame");
			addGameState("InGame");
			game = new Game(new JGPoint(960, 640));
			initGame();
		}
		else if (getKey('N')){
			System.exit(0);
		}		
	}
	
	public void paintFrameGameOver(){
		setColor(JGColor.white);
		
		// draw info about winner
		String WON = "Robots";
		if (board.dragonsWon()) WON = "Dragons";
		
		WON = WON.concat(" have won the game!");
		drawString(WON, 450, 250, 0);
		
		// draw new game info
		drawString("To play new game press Y or press N to exit", 450, 300, 0);
	}
	
	private void killUnits() {		
		for (int i = 0; i < 5; i++){
			if (!board.robots1[i].isAlive())
				removeObject(board.robots1[i]);
			
			if (!board.dragons1[i].isAlive())
				removeObject(board.dragons1[i]);			
		}
		
		for (int i = 0; i < 2; i++){
			if (!board.robots2[i].isAlive())
				removeObject(board.robots2[i]);
			
			if (!board.dragons2[i].isAlive())
				removeObject(board.dragons2[i]);			
		}
		
		for (int i = 0; i < 2; i++){
			if (!board.robots3[i].isAlive())
				removeObject(board.robots3[i]);
			
			if (!board.dragons3[i].isAlive())
				removeObject(board.dragons3[i]);			
		}
		
		for (int i = 0; i < 2; i++){
			if (!board.robots4[i].isAlive())
				removeObject(board.robots4[i]);
			
			if (!board.dragons4[i].isAlive())
				removeObject(board.dragons4[i]);			
		}
		
		for (int i = 0; i < 2; i++){
			if (!board.robotKing.isAlive())
				removeObject(board.robotKing);
			
			if (!board.dragonKing.isAlive())
				removeObject(board.dragonKing);			
		}
	}

	public void checkAllowed(boolean secondStep) {
		if (!board.selected){
			Field f = board.getField(getMouseX(), getMouseY());
			int cid = board.getCID(f.i, f.j);
			
			if (cid != 0 && (board.firstToPlay == board.turn && board.isDragon(cid))
					|| (board.firstToPlay != board.turn && board.isRobot(cid))){
				// unit has been selected
				board.selected = true;
				board.setAllowed(Unit.getUnitFromCID(cid));
				// check if robot1 or dragon1
		/*		boolean found = false;
				for (int i = 0; i < 5 && !found; i++){
					if (cid == board.robots1[i].getCID()){
						found = true;
						board.setAllowed(board.robots1[i]);
					}
					else if (cid == board.dragons1[i].getCID()){
						found = true;
						board.setAllowed(board.dragons1[i]);
					}
				}
				
				// check if robot2 or dragon2
				for (int i = 0; i < 2 && !found; i++){
					if (cid == board.robots2[i].getCID()){
						found = true;
						board.setAllowed(board.robots2[i]);
					}
					else if (cid == board.dragons2[i].getCID()){
						found = true;
						board.setAllowed(board.dragons2[i]);
					}
				}
				
				// check if robot3 or dragon3
				for (int i = 0; i < 2 && !found; i++){
					if (cid == board.robots3[i].getCID()){
						found = true;
						board.setAllowed(board.robots3[i]);
					}
					else if (cid == board.dragons3[i].getCID()){
						found = true;
						board.setAllowed(board.dragons3[i]);
					}
				}
				
				// check if robot4 or dragon4
				for (int i = 0; i < 2 && !found; i++){
					if (cid == board.robots4[i].getCID()){
						found = true;
						board.setAllowed(board.robots4[i]);
					}
					else if (cid == board.dragons4[i].getCID()){
						found = true;
						board.setAllowed(board.dragons4[i]);
					}
				}
				
				if (!found){
					// check if robotKing or dragonKing
					if (cid == board.robotKing.getCID()){
						found = true;
						board.setAllowed(board.robotKing);
					}
					else if (cid == board.dragonKing.getCID()){
						found = true;
						board.setAllowed(board.dragonKing);
					}
				}*/
			}
		}
		else {	// unit is selected
			if (secondStep){
				board.setAllowed(board.getSelectedUnit());
			}
			else {
				Field f = board.getField(getMouseX(), getMouseY());
				int cid = board.getCID(f.i, f.j);
				
				if (cid == board.getSelectedUnitID()){
					board.selected = false;
					board.resetAllowed();
				}
				else if (board.isAllowed(f.i, f.j)){
					// if clicked on allowed field
					// move unit to that field
					Unit unit = board.getSelectedUnit();
					unit.goTo(f);
				}
			}
		}	
	}
}
